// pages/myorder/myorderlist/myorderlist.js
import {
  getOrders,
  trashOrder,
  printerSend,
  delOrder,
  tiXingFaHuo,
  orderSearch
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curTab: 1,
    page_no: 1,
    dataSource: [],
    proxies: [],
    curProxie: "",
    printering: false,
    isShowDetail: false,
    keywork: "", //搜索关键词
    total:""
  },
  onLoad: function() {
    this.setData({
      isShowDetail: wx.getStorageSync('isShowDetail') ? wx.getStorageSync('isShowDetail') : false
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    let {
      curTab,
      page_no,
      curProxie
    } = this.data;
    let self = this;
    if (page_no == 1) {
      getOrders(curTab, page_no, curProxie, (r) => {
        if (r.code == 0) {
          self.setData({
            dataSource:r.data.orders,
            head: r.data.head,
            proxies: r.proxies,
            last_page: r.pager.last_page,
            total: r.pager.total
          })
        } else {
          errorMsg(r.message)
        }
      })
    }

  },
  changeViewList: function() {
    wx.setStorageSync('isShowDetail', !this.data.isShowDetail)
    this.setData({
      isShowDetail: !this.data.isShowDetail
    })


  },
  onChange(e) {  
    let self = this;
    let page_no = 1;
    let curTab = (e.detail.index+1);
    getOrders(curTab, page_no, "", (r) => {
      if (r.code == 0) {
        self.setData({
          curTab: curTab,
          page_no: page_no,
          dataSource: r.data.orders,
          proxies: r.proxies,
          last_page: r.pager.last_page,
          curProxie: "",
          total: r.pager.total,
          head: r.data.head
        })
      } else {
        errorMsg(r.message)
      }
    }) 
  },
  changeTab: function(e) {
    let self = this;
    let page_no = 1;
    let curTab = e.currentTarget.dataset.index;
    getOrders(curTab, page_no, "", (r) => {
      if (r.code == 0) {
        self.setData({
          curTab: curTab,
          page_no: page_no,
          dataSource: r.data.orders,
          proxies: r.proxies,
          last_page: r.pager.last_page,
          curProxie: "",
          total:r.pager.total,
          head:r.data.head
        })
      } else {
        errorMsg(r.message)
      }
    })

  },
  goSearch:function(){
    wx.navigateTo({
      url: '../myorderlistsearch/myorderlistsearch',
    })
  },
  goSendOrder: function(e) {
    let {
      curTab
    } = this.data;
    if(curTab == 6){
      curTab = e.currentTarget.dataset.curtab
    }
    let oid = e.currentTarget.dataset.oid
    if (curTab == 1) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 2) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 3) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 4) {
      wx.navigateTo({
        url: '../orderdetail/orderdetail?oid=' + oid,
      })
    } else if (curTab == 5) {
      wx.navigateTo({
        url: '../orderdetail/orderdetail?oid=' + oid,
      })
    }
  },
  //删除订单
  delOrder: function(e) {
    let id = e.currentTarget.dataset.id
    let {
      curTab
    } = this.data;
    let self = this;

    wx.showModal({
      title: '删除订单',
      content: '确定要删除该订单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          delOrder(id, curTab, (r) => {
            if (r.code == 0) {
              self.setData({
                dataSource: r.data.orders,
                page_no: 1,
                proxies: r.proxies
              })
              errorMsg("删除成功")
            } else {
              errorMsg(r.message);
            }
          })

        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  //订单补打
  orderBuDa: function(e) {
    let id = e.currentTarget.dataset.id
    let {
      printering
    } = this.data;
    let self = this;

    wx.showModal({
      title: '快递面单补打',
      content: '确定要补打该订单的快递面单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#46d5a4', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          if (!printering) {
            self.setData({
              printering: true,
            })
            printerSend(id, (r) => {
              if (r.code == 0) {
                self.setData({
                  printering: false,
                })
                errorMsg(r.message);
              } else {
                self.setData({
                  printering: false,
                })
                errorMsg(r.message);
              }
            })
          }
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })



  },
  getkeywork: function(e) {
    this.setData({
      keywork: e.detail.value
    })
  },
  //作废订单
  trashOrder: function(e) {
    let id = e.currentTarget.dataset.id
    let {
      curTab
    } = this.data;
    let self = this;
    wx.showModal({
      title: '作废订单',
      content: '确定要作废该订单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          trashOrder(id, curTab, (r) => {
            debugger;
            if (r.code == 0) {
              self.setData({
                dataSource: r.data.orders,
                page_no: 1,
                proxies: r.proxies
              })
              errorMsg("作废成功")
            } else {
              errorMsg(r.message)
            }

          })

        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  //图片预览
  previewImage: function(e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current[0], // 当前显示图片的http链接		  	
      urls: current // 需要预览的图片http链接列表	
    })
  },
  //下啦刷新
  onPullDownRefresh: function() {
    let self = this;
    let {
      curTab,
      curProxie
    } = this.data;
    let page_no = 1;

    getOrders(curTab, page_no, curProxie, (r) => {
      if (r.code == 0) {
        self.setData({
          page_no: page_no,
          dataSource: r.data.orders,
          proxies: r.proxies,
          total: r.pager.total
        })
        wx.stopPullDownRefresh()
      } else {
        errorMsg(r.message)
      }
    })
  },
  //上拉加载
  onReachBottom: function() {
    var self = this;
    let {
      curTab,
      page_no,
      last_page,
      curProxie
    } = this.data;

    if (last_page <= page_no) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page_no = page_no + 1;
      getOrders(curTab, page_no, curProxie, (r) => {
        if (r.code == 0) {
          self.setData({
            page_no: page_no,
            dataSource: self.data.dataSource.concat(r.data.orders),
            last_page: r.pager.last_page,
            proxies: r.proxies
          })
        } else {
          errorMsg(r.message)
        }
      })
    }

  },
  //提醒发货
  onTiXing: function(e) {
    let id = e.currentTarget.dataset.id;
    tiXingFaHuo(id, (r) => {
      if (r.code == 0) {
        errorMsg(r.message)
      } else {
        errorMsg(r.message)
      }
    })

  },
  search: function() {
    let self = this;
    let {
      keywork,
      curTab,
    } = this.data;
   let  page_no = 1;
      orderSearch(keywork, curTab,page_no, (r) => {
        if (r.code == 0) {
          self.setData({
            dataSource: r.data.orders,
            proxies: r.proxies,
            last_page: r.pager.last_page,
            page_no: page_no,
          })
        } else {
          errorMsg(r.message)
        }
      }) 
  },
  onProFilter: function(e) {
    let id = e.currentTarget.dataset.id;
    let self = this;
    let {
      curTab,
    } = this.data;
    let page_no = 1;
    getOrders(curTab, page_no, id, (r) => {
      if (r.code == 0) {
        self.setData({
          page_no: page_no,
          dataSource: r.data.orders,
          last_page: r.pager.last_page,
          proxies: r.proxies,
          curProxie: id
        })
      } else {
        errorMsg(r.message)
      }
    })

  },
  onMore: function() {
    wx.navigateTo({
      url: '../usercheck/usercheck?state=' + this.data.curTab,
    })
  },
  goBatchPrinter: function() {
    wx.navigateTo({
      url: '../batchprinter/batchprinter',
    })
  },
  goBatchAccept:function(){
    wx.navigateTo({
      url: '../batchaccept/batchaccept',
    })
  }
})